import {
    IFormAbility,
    IFormController,
    IFormDetailAbility,
    IFormDetailController,
    IFormDetailControllerParams,
    IFormStore,
    IParam,
} from '@/core/interface';

/**
 * 表单成员控制器基类
 *
 * @export
 * @class FormDetailController
 * @implements {IFormDetailController<A>}
 * @template A
 */
export class FormDetailController<A extends IFormDetailAbility>
    implements IFormDetailController<A>
{
    /**
     * 成员是否必填
     *
     * @type {boolean}
     * @memberof FormDetailController
     */
    private $required = false;

    /**
     * 表单
     *
     * @private
     * @type {IFormController<IFormStore, IFormAbility>}
     * @memberof FormDetailController
     */
    private form: IFormController<IFormStore, IFormAbility>;

    /**
     * 成员模型数据
     *
     * @protected
     * @type {IParam}
     * @memberof FormDetailController
     */
    protected readonly model: IParam = {};

    /**
     * 成员能力块
     *
     * @protected
     * @type {IFormDetailAbility}
     * @memberof FormDetailController
     */
    protected ability!: IFormDetailAbility;

    /**
     * 控制器名称
     *
     * @type {string}
     * @memberof FormDetailController
     */
    name!: string;

    /**
     * 控制器标识
     *
     * @type {string}
     * @memberof FormDetailController
     */
    codeName: string;

    /**
     * 成员类型
     *
     * @type {string}
     * @memberof FormDetailController
     */
    detailType!: string;

    /**
     * 成员标题
     *
     * @protected
     * @type {string}
     * @memberof FormDetailController
     */
    caption!: string;

    /**
     * 成员是否显示
     *
     * @type {boolean}
     * @memberof FormDetailController
     */
    visible = true;

    /**
     * 成员是否禁用
     *
     * @type {boolean}
     * @memberof FormDetailController
     */
    disabled = false;

    /**
     * 容器显示的更多项
     *
     * @type {string[]}
     * @memberof FormDetailController
     */
    showMoreItems: string[] = [];

    /**
     * 启用模式 (不启用 | 新建 | 更新 | 启用)
     *
     * @type {(0 | 1 | 2 | 3)}
     * @memberof FormDetailController
     */
    enableCond: 0 | 1 | 2 | 3 = 3;

    /**
     * 显示更多模式 (无 | 受控内容 | 管理容器 )
     *
     * @protected
     * @type {(0 | 1 | 2)}
     * @memberof FormDetailController
     */
    readonly showMoreMode: 0 | 1 | 2 = 0;

    /**
     * 忽略值输入
     *
     * @type {(0 | 1| 2| 3 | 4)}
     * @memberof FormDetailController
     */
    ignoreInput: 0 | 1 | 2 | 3 | 4 = 0;

    /**
     * 表单按钮行为
     *
     * @type {IParam}
     * @memberof FormDetailController
     */
    uIAction?: IParam;

    /**
     * 表单分组行为组
     *
     * @type {IParam}
     * @memberof FormDetailController
     */
    uIActionGroup?: IParam;

    /**
     * 新建默认值项
     *
     * @type {IParam[]}
     * @memberof FormDetailController
     */
    createDefaultItems: IParam[];

    /**
     * 更新默认值项
     *
     * @type {IParam[]}
     * @memberof FormDetailController
     */
    updateDefaultItems: IParam[];

    /**
     * 值规则
     *
     * @type {IParam[]}
     * @memberof FormDetailController
     */
    valueRules: IParam[] = [];

    /**
     * 错误提示
     *
     * @type {string}
     * @memberof FormDetailController
     */
    errorMessage:string = '';

    /**
     * 重复器子成员
     *
     * @type {IParam[]}
     * @memberof FormDetailController
     */
    childrenDetailModel: IParam[] = [];

    /**
     * 父是否为重复器
     *
     * @type {boolean}
     * @memberof FormDetailController
     */
    parentIsRepeater: boolean = false;

    /**
     * 父重复器标识名称
     *
     * @type {string}
     * @memberof FormDetailController
     */
    parentCodeName: string = '';

    /**
     * 行为类型
     *
     * @type {string}
     * @memberof FormDetailController
     */
    actionType: string = '';

    /**
     * 重复器子项成员标识
     *
     * @type {string[]}
     * @memberof FormDetailController
     */
    childrenCodeName: string[] = [];

    /**
     * Creates an instance of FormDetailController.
     * @param {IFormDetailControllerParams} params
     * @memberof FormDetailController
     */
    constructor(params: IFormDetailControllerParams) {
        this.model = params.model;
        this.name = params.model.name;
        this.codeName = params.model.codeName;
        this.detailType = params.model.detailType;
        this.actionType = params.model.actionType;
        this.showMoreMode = params.model.showMoreMode;
        this.showMoreItems = params.model.showMoreItems || [];
        this.uIAction = params.model.uIAction;
        this.uIActionGroup = params.model.uIActionGroup;
        this.enableCond = params.model.enableCond;
        this.createDefaultItems = params.model.createDefaultItems || [];
        this.updateDefaultItems = params.model.updateDefaultItems || [];
        this.parentIsRepeater = params.model.parentIsRepeater;
        this.parentCodeName = params.model.parentCodeName;
        this.form = params.form;
        this.childrenCodeName = params.model.childrenCodeName || [];
        this.detailInit();
    }

    /**
     * 成员初始化
     *
     * @memberof FormDetailController
     */
    detailInit() {
        this.visible = this.model.visible || true;
        this.$required = this.model.required || false;
        this.disabled = this.model.disabled || false;
        if (this.model.groupLogics) {
            const panelVisible = this.model.groupLogics.find(
                (logic: IParam) => logic.logicCat === 'PANELVISIBLE'
            );
            const itemBlank = this.model.groupLogics.find(
                (logic: IParam) => logic.logicCat === 'ITEMBLANK'
            );
            const itemEnable = this.model.groupLogics.find(
                (logic: IParam) => logic.logicCat === 'ITEMENABLE'
            );
            if (panelVisible) {
                this.visible = false;
            }
            if (itemBlank) {
                this.$required = false;
            }
            if (itemEnable) {
                this.disabled = true;
            }
        }
    }

    /**
     * get 必填
     *
     * @type {boolean}
     * @memberof FormDetailController
     */
    get required(): boolean {
        return this.$required;
    }

    /**
     * set 必填
     *
     * @memberof FormDetailController
     */
    set required(value: boolean) {
        this.$required = value;
        if (this.valueRules && this.valueRules.length > 0) {
            this.valueRules.forEach((rule: IParam) => {
                if (rule.hasOwnProperty('required')) {
                    rule.required = this.$required;
                    // 必填 - > 非必填 清除必填校验信息
                    if (!this.$required) {
                        this.form.formValidate([this.name]);
                    }
                }
            })
        }
    }

    /**
     * 获取能力
     *
     * @template A
     * @return {*}  {A}
     * @memberof FormDetailController
     */
    getAbility(): A {
        return this.ability as A;
    }

    /**
     * 设置能力
     *
     * @param {IFormDetailAbility} ability
     * @memberof FormDetailController
     */
    setAbility(ability: IFormDetailAbility): void {
        this.ability = ability;
    }

    /**
     * 获取模型
     *
     * @template T
     * @return {*}  {T}
     * @memberof FormDetailController
     */
    getModel(): IParam {
        return this.model;
    }
}
